<template>
  <a-form-model
    ref="settingForm"
    :model="settingForm"
    :rules="settingRules"
    :label-col="{ span: 4 }"
    :wrapper-col="{ span: 14 }"
  >
  <a-form-model-item label="是否开启" prop="is_enable">
    <a-radio-group v-model="settingForm.is_enable">
      <a-radio value="1"> 开启 </a-radio>
      <a-radio value="0"> 关闭 </a-radio>
    </a-radio-group>
  </a-form-model-item>
  <a-form-model-item label="返佣比例" prop="referral_rebate">
    <a-input addon-after="%" placeholder="请输入返佣比例" v-model="settingForm.referral_rebate" />
  </a-form-model-item>

  <a-form-model-item :wrapper-col="{ span: 24 }">
    <a-divider style="margin:0 0 40px 0;"/>
    <div style="display: flex; justify-content: center;">
      <a-button :loading="btnLoading" type="primary" @click="submitForm" style="margin-right: 8px">
        保存
      </a-button>
      <a-button @click="$router.back()"> 取消 </a-button>
    </div>
  </a-form-model-item>
</a-form-model>
</template>

<script>
import { editShopReferrer,saasReferralDefaultParams } from '@/api/saas/alliance'

export default {
  data() {
    return {
      settingForm: {
        referral_rebate: '',
        is_enable: '1',
      },
      settingRules: {
        referral_rebate: [{ required: true, message: '请输入返佣比例', trigger: 'blur' }],
        is_enable: [{ required: true, message: '请选择是否开启', trigger: 'change' }],
      },
    }
  },

  created() {
    this.getData()
  },

  methods:{
    getData () {
      this.loading = true
      saasReferralDefaultParams()
        .then((res) => {
          if (res.code == 0) {
            this.$nextTick(() => {
              this.settingForm = res.data
            })
          }
        })
        .catch((error) => {
          console.log(error)
        })
        .then(() => {
          this.loading = false
        })
    },

    submitForm () {
      this.$refs.settingForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          editShopReferrer({
            ...this.settingForm,
            store_id:'-1'
          })
            .then((res) => {
              if (res.code == 0) {
                this.$message.success('保存成功！')
              } else {
                this.$message.warning(res.msg ? res.msg : '保存失败！')
              }
            })
            .catch((error) => {
              console.log(error)
            })
            .then(() => {
              this.btnLoading = false
            })
        }
      })
    },
  }
}
</script>

<style lang="less" scoped>
</style>